package com.liuyk.commonlib.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.liuyk.commonlib.R;

/**
 * 带下拉刷新的activity
 * <p>
 * Created by liuyakui on 2020/5/4.
 */
public abstract class BaseSwipeRefreshWithLoadingActivity extends BaseLoadingActivity implements SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLoadingContent(R.layout.base_swipe_refresh_loading_layout);
        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh);
        mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    protected void setSwipeRefreshLoadingContent(int resId) {
        final View contentView = LayoutInflater.from(this).inflate(resId, mSwipeRefreshLayout, false);
        setSwipeRefreshLoadingContent(contentView);
    }

    protected void setSwipeRefreshLoadingContent(View view) {
        mSwipeRefreshLayout.addView(view);
    }

    protected boolean isRefreshing() {
        return mSwipeRefreshLayout.isRefreshing();
    }

    protected void stopRefresh() {
        if (isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    protected void startRefresh() {
        if (!isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void onRefresh() {

    }
}
